package com.tarena.elts.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;

import com.tarena.elts.entity.ExamInfo;
import com.tarena.elts.entity.QuestionInfo;

public class ExamFrame extends JFrame {
	private ClientContext clientContext;
	public void setClientContext(ClientContext clientContext) {
		this.clientContext = clientContext;
	}
	private JButton prev;
	private JButton next;
	private Option[] options = new Option[4];
	private JLabel examInfo;
	private JTextArea questionArea;
	private JLabel count;
	private JLabel timer;
	public ExamFrame() {
		this.init();
	}

	private void init() {
		this.setSize(600, 400);
		this.setLocationRelativeTo(null);
		this.add(createContentPanel());
		this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
	}

	private JPanel createContentPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(BorderLayout.NORTH, createTopPanel());
		panel.add(BorderLayout.CENTER, createCenterPanel());
		panel.add(BorderLayout.SOUTH, createEndPanel());
		return panel;
	}

	private JPanel createTopPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(BorderLayout.NORTH, createTitlePanel());
		panel.add(BorderLayout.SOUTH, createTipPanel());
		return panel;
	}

	private JPanel createTitlePanel() {
		JPanel panel = new JPanel(new BorderLayout());
		JLabel label = createImgIcon("exam_title.png");
		panel.add(BorderLayout.NORTH, label);
		return panel;
	}

	private JPanel createTipPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		JLabel label = new JLabel("java exam", JLabel.CENTER);
		this.examInfo=label;
		panel.add(BorderLayout.SOUTH, label);
		return panel;
	}

	private JScrollPane createCenterPanel() {
		JScrollPane pane = new JScrollPane();
		pane.setBorder(new TitledBorder("题目"));
		JTextArea textArea = new JTextArea();
		textArea.setText("问题\nA.\nB.");
		this.questionArea=textArea;
		textArea.setLineWrap(true);
		textArea.setEditable(false);
		pane.add(textArea);
	  pane.getViewport().add(textArea);
		return pane;
	}

	private JPanel createEndPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(BorderLayout.NORTH, createOptionPanel());
		panel.add(BorderLayout.SOUTH, createLastPanel());
		return panel;
	}

	private JPanel createOptionPanel() {
		JPanel panel = new JPanel(new FlowLayout());
		Option a = new Option(0, "A");
		Option b = new Option(1, "B");
		Option c = new Option(2, "C");
		Option d = new Option(3, "D");
		options[0] = a;
		options[1] = b;
		options[2] = c;
		options[3] = d;
		panel.add(a);
		panel.add(b);
		panel.add(c);
		panel.add(d);
		return panel;
	}

	private JPanel createLastPanel() {
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(BorderLayout.WEST, createQuestionPanel());
		panel.add(BorderLayout.CENTER, createItemPanel());
		panel.add(BorderLayout.EAST, createTimePanel());
		return panel;
	}

	private JPanel createQuestionPanel() {
		JPanel panel = new JPanel();
		JLabel label = new JLabel("question");
		this.count=label;
		panel.add(label);
		return panel;
	}

	private JPanel createItemPanel() {
		JPanel panel = new JPanel(new FlowLayout());
	  prev = new JButton("前一个");
	  next = new JButton("后一个");
		JButton send = new JButton("交卷");
		prev.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				clientContext.prev();
			}
		});
		next.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				clientContext.next();
			}
		});
		send.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				clientContext.send();
			}
		});
		panel.add(prev);
		panel.add(next);
		panel.add(send);
		return panel;
	}

	private JPanel createTimePanel() {
		JPanel panel = new JPanel();
		JLabel label = new JLabel("time");
		this.timer=label;
		panel.add(label);
		return panel;
	}

	private JLabel createImgIcon(String img) {
		ImageIcon ico = new ImageIcon(this.getClass().getResource(img));
		JLabel label = new JLabel(ico);
		return label;
	}

	class Option extends JCheckBox {
		private static final long serialVersionUID = -5357210068923979044L;
		int value;

		public Option(int val, String txt) {
			super(txt);
			this.value = val;
		}
	}
	/**
	 * 更新考试界面，显示考试信息，和当前题目信息
	 * @param examInfo
	 * @param questionInfo
	 */
	public void updateView(ExamInfo examInfo ,QuestionInfo questionInfo){
		this.examInfo.setText(examInfo.toString());
		questionArea.setText(questionInfo.toString());
		
		int index = questionInfo.getQuestionIndex()+1;
		int count = examInfo.getQuestionCount();
		String str = "总共 "+count+ "题 第"+index+"题";
		this.count.setText(str);
		
		List<Integer> answers = questionInfo.getUserAnswers();
		
		updateOptions(answers);
		updateButtons(questionInfo.getQuestionIndex(),examInfo.getQuestionCount());
	}
	private void updateOptions(List<Integer> answers) {
		// TODO Auto-generated method stub
		for(Option option:options){
			option.setSelected(answers.contains(option.value));
		}
	}

	private void updateButtons(int index,int count) {
		// TODO Auto-generated method stub
		prev.setEnabled(index!=0);
		next.setEnabled(index!=count-1);
	}

	public List<Integer> getUserAnswers(){
		List<Integer> list = new ArrayList<Integer>();
		for(Option option : options){
			if(option.isSelected()){
				list.add(option.value);
			}
		}
		return list;
	}
	public void updateTimer(long h,long m,long s){
		if(h==0 && m<=5){
			this.setForeground(Color.red);
		}
		String str = h+":"+m+":"+s;
		timer.setText(str);
	}
}
